package com.jkgh.remedium.objects;

import com.jkgh.asin.StateMachine;
import com.jkgh.asin.buffers.DataConsumer;
import com.jkgh.remedium.RemediumHost;
import com.jkgh.remedium.RemediumServer;
import com.jkgh.remedium.objects.messages.RemediumInMessage;
import com.jkgh.remedium.objects.messages.RemediumOutMessage;
import com.jkgh.remedium.states.Callback;

public abstract class RemediumMethodObject extends RemediumObject {
	
	protected final int objectID;
	private final int hostID;
	
	public RemediumMethodObject(int hostID, int objectID, RemediumServer server) {
		this.hostID = hostID;
		this.objectID = objectID;
		
		RemediumHost host = server.getHost(hostID);
		host.registerMethodObject(objectID, this);
	}
	
	public RemediumMethodObject(RemediumServer server) {
		this.hostID = 0;
		this.objectID = server.nextObjectID();
		
		server.registerMethodObject(objectID, this);
	}

	public abstract void call(StateMachine machine, RemediumInMessage message, int callID, int methodID, Callback callback);
	
	@Override
	public void produceContent(DataConsumer consumer, RemediumOutMessage message) {
		consumer.consumeInt(hostID);
		consumer.consumeInt(objectID);
	}
}
